package com.common.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.constant.CommonConstant;
import com.common.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


public class BaseController<T> {

	@Autowired
	private HttpServletRequest request;
	
	
	/***
	 * session获取
	 * @return
	 */
	public HttpSession getSession(){
		return request.getSession();
	}
	
	/**
	 * 获取当前登录用户信息
	 * @return
	 */
	public LoginUser getUser() {
		Object obj = getSession().getAttribute(CommonConstant.LOGIN_USER);
		return (obj != null) ? (LoginUser)obj : null;
	}
	
	/**
	 * 清除当前登录用户信息-
	 * @return
	 */
	public void removeUser() {
		getSession().setMaxInactiveInterval(0);
		getSession().removeAttribute(CommonConstant.LOGIN_USER);
	}

	/**
	 * 返回存在session中的对象
	 * @param key
	 * @return
	 */
	public Object getSessionValue(String key) {
		HttpSession session = getSession();
		Object object = session.getAttribute(key);
		return object;
	}
	
	/**
	 * 返回存在session中的对象
	 * @param key
	 * @return
	 */
	public void setSessionValue(String key, Object object) {
		HttpSession session = getSession();
		session.setAttribute(key, object);
	}
	
	/**
	 * 返回分页
	 * @return
	 */
	public Page<T> getPage() {
		String page = request.getParameter("page");
		String limit = request.getParameter("limit");
		Integer p = page==null?1:Integer.parseInt(page);
		Integer l = limit==null?10:Integer.parseInt(limit);
		return new Page<>(p, l);
	}

	/**
	 * 获取计算后的分页
	 * @return
	 */
	public Integer getPage(Integer page, Integer limit) {
		return ((limit * page)) - limit;
	}

	/**
	 * 获取计算后的总数
	 * @return
	 */
	public Integer getLimit(Integer page, Integer limit) {
		return limit * page;
	}





}
